﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NguoiDung
{
    public partial class TaoHoSoBuoc4 : System.Web.UI.Page
    {
        private class SoNamKN
        {
            public int Nam { set; get; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["hoso"] != "")
                {
                    var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);
                    LoadData(hosoId);
                }
                else
                {
                    Response.Redirect("/");
                }
            }
        }

        private void LoadData(int hosoId)
        {
            if (Session["NguoiDung"] != null)
            {

                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                if (user.tbl_HS_HoSos.Any(r => r.HoSoId == hosoId))
                {
                    List<SoNamKN> lst = new List<SoNamKN>();
                    //Kinh nghiệm
                    for (int i = 0; i < 50; i++)
                    {
                        lst.Add(new SoNamKN {Nam = i});
                    }

                    droSoNam.DataSource = lst;
                    droSoNam.DataTextField = "Nam";
                    droSoNam.DataValueField = "Nam";
                    droSoNam.DataBind();

                    ListItem item3 = new ListItem();
                    item3.Text = "Chọn số năm sử dụng";
                    item3.Value = "-1";
                    item3.Selected = true;
                    droSoNam.Items.Add(item3);


                    droNhomKiNang.DataSource = BLL.Admin.DMNhomKiNangHelper.ListNhomKiNang();
                    droNhomKiNang.DataTextField = "TenNhomKiNang";
                    droNhomKiNang.DataValueField = "NhomKiNangId";
                    droNhomKiNang.DataBind();
                    ListItem item1= new ListItem();
                    item1.Text = "Chọn nhóm kĩ năng";
                    item1.Value = "-1";
                    item1.Selected = true;
                    droNhomKiNang.Items.Add(item1);
                    //
                    droKiNang.DataSource = BLL.Admin.DMKiNangHelper.ListKiNangTheoNhomKiNang(int.Parse(droNhomKiNang.SelectedValue));
                    droKiNang.DataTextField = "TenKiNang";
                    droKiNang.DataValueField = "KiNangId";
                    droKiNang.DataBind();
                    ListItem item2 = new ListItem();
                    item2.Text = "Chọn kĩ năng";
                    item2.Value = "-1";
                    item2.Selected = true;
                    droKiNang.Items.Add(item2);

                    var kiNang = BLL.NguoiDung.HoSo.LoadKiNang(hosoId);
                    rptKiNang.DataSource = kiNang;
                    rptKiNang.DataBind();
                }
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            Response.Redirect("/nguoi-dung/tu-ho-so.html");
        }

        protected void btSaveAndContinue_Click(object sender, EventArgs e)
        {
            var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);
            Response.Redirect("/nguoi-dung/" + hosoId + "tao-ho-so-buoc5.html");
        }

        protected void btSaveKinhNghiem_Click(object sender, EventArgs e)
        {
            var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);

            if(droNhomKiNang.SelectedValue=="-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn nhóm kĩ năng!');", true);
                droNhomKiNang.Focus();
                return;
            }

            if (droKiNang.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn kĩ năng!');", true);
                droKiNang.Focus();
                return;
            }

            if (droCapDo.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn cấp độ!');", true);
                droCapDo.Focus();
                return;
            }

            if (droSoNam.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn số năm sử dụng!');", true);
                droSoNam.Focus();
                return;
            }

            BLL.NguoiDung.HoSo.AddKiNangLamViec(hosoId, Convert.ToInt32(droNhomKiNang.SelectedValue),
                                                Convert.ToInt32(droKiNang.SelectedValue),
                                                Convert.ToInt32(droCapDo.SelectedValue),
                                                Convert.ToInt32(droSoNam.SelectedValue));
            var kiNang = BLL.NguoiDung.HoSo.LoadKiNang(hosoId);
            rptKiNang.DataSource = kiNang;
            rptKiNang.DataBind();
            
        }

        protected void rptKiNang_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Xoa")
            {
                var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);
                int kiNangId = int.Parse(e.CommandArgument.ToString());
                BLL.NguoiDung.HoSo.XoaKiNangLamViec(kiNangId);
                var hocvans = BLL.NguoiDung.HoSo.LoadKiNang(hosoId);
                rptKiNang.DataSource = hocvans;
                rptKiNang.DataBind();
            }
        }

        protected void droNhomKiNang_SelectedIndexChanged(object sender, EventArgs e)
        {
            droKiNang.DataSource = BLL.Admin.DMKiNangHelper.ListKiNangTheoNhomKiNang(int.Parse(droNhomKiNang.SelectedValue));
            droKiNang.DataTextField = "TenKiNang";
            droKiNang.DataValueField = "KiNangId";
            droKiNang.DataBind();
        }
    }
}